package com.engine.service;

import com.engine.core.dao.CodeResourceMapper;
import com.engine.core.gen.Global;
import com.engine.core.interceptor.JdbcCommandContext;
import com.engine.core.interceptor.ResponseCommand;
import com.engine.core.util.*;
import com.engine.dialect.DialectType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Service
public class DbDataService {

    @Autowired
    private Global global;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private CodeResourceMapper codeResourceMapper;
    /**
     * @return
     */
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        Map<String,Object> params=QueryUtil.request2Map(request);
        String action=(String)params.get(QueryUtil.ACTION);
        params.put(QueryUtil.QUERY_METHOD,request.getMethod());
        QueryActionEnum actionEnum=QueryActionEnum.get(action);
        if(actionEnum!=null){
            try {
                DialectType dialectType= (DialectType) DialectType.newInstance(global.getDbType());
                Class<? extends ResponseCommand> commandClass=(Class<? extends ResponseCommand>)actionEnum.getCommand();
                ResponseCommand abstractCommand=(ResponseCommand)QueryUtil.getCommand(commandClass,null,params);
                return  abstractCommand.execute(new JdbcCommandContext(global,jdbcTemplate,applicationContext,dialectType));
            } catch (Exception e) {
                System.err.println(e);
               return  Response.static_fail(QueryUtil.ResponseCode.ERROR);
            }
        }
        return Response.static_fail(QueryUtil.ResponseCode.ERROR);
    }

    public Response query(HttpServletRequest request, HttpServletResponse response,String action) {
        Map<String,Object> params=QueryUtil.request2Map(request);
        params.put(QueryUtil.QUERY_METHOD,request.getMethod());
        QueryActionEnum actionEnum=QueryActionEnum.get(action);
        if(actionEnum!=null){
            try {
                DialectType dialectType= (DialectType) DialectType.newInstance(global.getDbType());
                Class<? extends ResponseCommand> commandClass=(Class<? extends ResponseCommand>)actionEnum.getCommand();
                ResponseCommand abstractCommand=(ResponseCommand)QueryUtil.getCommand(commandClass,null,params);
                return  abstractCommand.execute(new JdbcCommandContext(global,jdbcTemplate,applicationContext,dialectType));
            } catch (Exception e) {
                System.err.println(e);
                return  Response.static_fail(QueryUtil.ResponseCode.ERROR);
            }
        }
        return Response.static_fail(QueryUtil.ResponseCode.ERROR);
    }

    public  Response putSave(HttpServletRequest request, HttpServletResponse response){
         Map<String,Object> params=QueryUtil.request2Map(request);
         String action= Utils.nullDefault((String)params.get(QueryUtil.ACTION),"save");
         params.put(QueryUtil.QUERY_METHOD,request.getMethod());
         UpdateActionEnum actionEnum=UpdateActionEnum.get(action);
         if(actionEnum!=null){
            try {
                DialectType dialectType= (DialectType) DialectType.newInstance(global.getDbType());
                Class<? extends ResponseCommand> commandClass=(Class<? extends ResponseCommand>)actionEnum.getCommand();
                ResponseCommand abstractCommand=(ResponseCommand)QueryUtil.getCommand(commandClass,null,params);
                return  abstractCommand.execute(new JdbcCommandContext(global,jdbcTemplate,applicationContext,dialectType));
            } catch (Exception e) {
                System.err.println(e);
                return  Response.static_fail(QueryUtil.ResponseCode.ERROR);
            }
         }
         return Response.static_fail(QueryUtil.ResponseCode.ERROR);

    }

    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        Map<String,Object> params=QueryUtil.request2Map(request);
        String action= Utils.nullDefault((String)params.get(QueryUtil.ACTION),"delete");
        params.put(QueryUtil.QUERY_METHOD,request.getMethod());
        UpdateActionEnum actionEnum=UpdateActionEnum.get(action);
        if(actionEnum!=null){
            try {
                DialectType dialectType= (DialectType) DialectType.newInstance(global.getDbType());
                Class<? extends ResponseCommand> commandClass=(Class<? extends ResponseCommand>)actionEnum.getCommand();
                ResponseCommand abstractCommand=(ResponseCommand)QueryUtil.getCommand(commandClass,null,params);
                return  abstractCommand.execute(new JdbcCommandContext(global,jdbcTemplate,dialectType));
            } catch (Exception e) {
                System.err.println(e);
                return  Response.static_fail(QueryUtil.ResponseCode.ERROR);
            }
        }
        return Response.static_fail(QueryUtil.ResponseCode.ERROR);
    }

    public Response refreshResource() {
        Response response =  Response.static_success("执行成功!");
        try {
            List<String> tables= codeResourceMapper.selectRefresh();
            for (int i = 0; i < tables.size(); i++) {
                String tableName=tables.get(i);
                int rows=codeResourceMapper.insertTableResource(tableName);
                int resId= codeResourceMapper.getIdByName(tableName);
                rows=codeResourceMapper.deleteByName(tableName);
                rows=codeResourceMapper.insertTableColumn(tableName,resId);
                response=rows>0 ? Response.static_success("执行成功!"):Response.static_fail("执行失败!",-1);
            }
        } catch (Exception e) {
            return  Response.static_fail("执行失败!",-1).setErr(e);
        }
        return response;
    }
}
